package com.ithe.canteen.service.impl;

import com.ithe.canteen.mapper.UserMapper;
import com.ithe.canteen.pojo.Customer;
import com.ithe.canteen.pojo.Manager;
import com.ithe.canteen.service.UserService;
import com.ithe.canteen.util.CreatIDUtil;
import com.ithe.canteen.util.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public Customer customerLogin(String username, String password) {
        return userMapper.queryCustomer(username, password);
    }

    @Override
    public String queryNickName(String customerID) {
        return userMapper.queryNickName(customerID);
    }

    @Override
    public boolean insertCustomer(String nickName,String userName, String password) {
        return userMapper.insertCustomer(CreatIDUtil.createID("US"), nickName, userName, password);
    }

    @Override
    public boolean queryUser(String userID) {
        return userMapper.queryUser(userID);
    }

    @Override
    public boolean updateFlavor(String customerID, String loveFood, String hateFood) {
        return userMapper.updateFlavor(customerID, loveFood, hateFood);
    }

    @Override
    public Manager managerLogin(String username, String password) {
        Manager manager = userMapper.queryManager(username, password);
        if (manager != null) {
            manager.setCreateTime(new Date());
            manager.setToken(TokenUtil.genToken(manager.getManagerID(), password));
            return manager;
        }
        return null;
    }
}
